/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-03 22:07:10  
 * Copyright (c) 2015- joysuch-版权所有     
 */

package com.joysuch.wwyt.core.controller;

import java.util.concurrent.locks.ReentrantLock;

import javax.validation.Valid;

import com.joysuch.wwyt.core.bean.SimpleSelectListRecursiveBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.entity.BaseRegion;
import com.joysuch.wwyt.core.service.BaseRegionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/base/region")
public class BaseRegionController {

    private static final Logger logger = LoggerFactory.getLogger(BaseRegionController.class);
    private SimpleSelectListRecursiveBean rootCache;
    private ReentrantLock lock = new ReentrantLock();

    @Autowired
    private BaseRegionService baseRegionService;

    @RequestMapping("add")
    public Object add(@Valid BaseRegion entity) {
        try {
            baseRegionService.add(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("创建失败", e);
            return ResultBean.fail(101, "创建失败");
        }
    }

    @RequestMapping("tree")
    public Object getTree() {
        try {
            if (rootCache == null) {
                lock.lock();
                if (rootCache == null) {
                    rootCache = baseRegionService.queryTree();
                }
                lock.unlock();
            }
            return ResultBean.success(rootCache.getChildren());
        } catch (Exception e) {
            logger.error("创建失败", e);
            return ResultBean.fail(101, "创建失败");
        }
    }


    @RequestMapping("reset-cache")
    public Object resetCache() {
        try {
            rootCache = null;
            return ResultBean.success(rootCache.getChildren());
        } catch (Exception e) {
            logger.error("创建失败", e);
            return ResultBean.fail(101, "创建失败");
        }
    }

    @RequestMapping("update")
    public Object update(@Valid BaseRegion entity) {
        try {
            if (entity.getId() == null) {
                return ResultBean.fail(102, "未指定主键");
            }
            baseRegionService.update(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("更新失败：#" + entity.getId(), e);
            return ResultBean.fail(101, "更新失败");
        }
    }

    @RequestMapping("page")
    public Object page(Pageable page) {
        try {
            Page<BaseRegion> data = baseRegionService.findByPage(page);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }







}
